# -*- coding: utf-8 -*-

# File Name： launch
# Description :
# Author : lirui
# create_date： 2022/9/12
# Change Activity:
import ml
from ml.third_party.csv.xls_utility import read_all_sheet_xls_mapping


def _main():
    """

    """
    mappings = {}
    xls_path_lst = ['resource/富滇银行新大楼-标签-设备位置统计信息_20220908_v1.8.xls', 'resource/背视图v1.0.xls']
    for p in xls_path_lst:
        xls_path = ml.join_relative_path(p)
        ml.logger.info(rf'xls path {xls_path}')
        sheets_mapping = read_all_sheet_xls_mapping(xls_path)
        sheets_mapping = _generate_labels(sheets_mapping)
        mappings.update(sheets_mapping)
    labels_collector = _collect_all_labels(mappings)
    _compare_labels(labels_collector)


def _compare_labels(labels_collector):
    """
    compare labels.
    Args:
        labels_collector:

    Returns:

    """
    for local_end, opposite_end in labels_collector.items():
        if opposite_end in labels_collector:
            if labels_collector[opposite_end] != local_end:
                ml.logger.error(rf'local_end:【{local_end}】,opposite_end: 【{opposite_end}】, mapping local_end:【{labels_collector[opposite_end]}】')


def _collect_all_labels(sheets_mapping):
    """

    Args:
        sheets_mapping:

    Returns:

    """
    label_collector = {}
    for sheet_name in sheets_mapping:
        mapping_lst = sheets_mapping[sheet_name]
        for mapping in mapping_lst:
            local_end = mapping['local_end']
            opposite_end = mapping['opposite_end']
            if local_end in label_collector:
                raise Exception(rf'{local_end} already exist.')
            label_collector[local_end] = opposite_end
    return label_collector


def _generate_labels(sheets_mapping):
    """

    Args:
        sheets_mapping:

    Returns:

    """
    for sheet_name in sheets_mapping:
        mapping_lst = sheets_mapping[sheet_name]
        for mapping in mapping_lst:
            local_end = f"{mapping['本端地址']}.{mapping['本端U数']}.{mapping['本端板卡编号']}"
            opposite_end = f"{mapping['对端地址']}.{mapping['对端U数']}.{mapping['对端板卡编号']}"
            if len(mapping['本端端口号'].strip()) > 0:
                local_end = local_end + '.' + mapping['本端端口号']
                opposite_end = opposite_end + '.' + mapping['对端端口号']
            mapping['local_end'] = local_end
            mapping['opposite_end'] = opposite_end
    return sheets_mapping


if __name__ == '__main__':
    _main()
